<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/5
 * Time: 16:48
 */

namespace app\controller\app;
use app\model\app\AppLoanOrder AS model;
use app\model\app\AppPayOrder AS pay;
use app\model\sys\SysAutoPayConfig AS config;
use app\model\sys\SysConfig AS sys_config;
use app\model\app\AppPay AS p;
use think\facade\Db;

class Order extends Base
{
    public function pay_info(){
        $info = pay::where('user_id',$this->userInfo->user_id)
            ->order('create_time DESC')
            ->limit(1)
            ->find();
        return $this->msg(['info' => $info]);
    }

    public function info(){
        $find = model::where('user_id',$this->userInfo->user_id)->find();
        return $this->msg($find);
    }

    public function create()
    {
        try {
            Db::startTrans();
            $count = model::where('user_id', $this->userInfo->user_id)->count();
            if ($count > 0)
                return $this->fail('order already exists');
            $post = input('post.');
            $post['kefu_id'] = $this->userInfo->kefu_id;
            $post['kefu_name'] = $this->userInfo->kefu->username;
            $post['user_id'] = $this->userInfo->user_id;
            $post['user_name'] = $this->userInfo->basic->fullname;
            $config = config::field('pay_title,pay_content,process_content,process_title')->where('status', 1)->find()->toArray();
            $auto_pay_modal = sys_config::where('param_key','auto_pay_modal')->value('param_value');
            foreach ($config AS $key => $value) $post[$key] = $value;
            $find = model::create($post);
            if($auto_pay_modal == 1){
                pay::where('user_id', $find->user_id)->where('status', 0)->save(['status' => 2]);
                $data = [
                    'order_id' => $find->id,
                    'user_id' => $find->user_id,
                    'name' => $find->basic->branch_name,
                    'kefu_id' => $find->kefu_id,
                    'kefu_name' => $find->kefu->username,
                    'pay_content' => $post['pay_content'],
                    'pay_title' => $post['pay_title'],
                    'pay_amount' => $post['pay_amount'],
                    'pay_name' => p::where('pay_ds', 1)->value('pay_name'),
                    'pay_account' => $find->pay_account,
                    'order_code' => 'C' . date('YmdHis') . rand(10000, 99999),
                    'email' => $find->user->email,
                    'phone' => $find->user->mobile,
                    'status' => 0,
                    'create_time' => date('Y-m-d H:i:s'),
                    'update_time' => date('Y-m-d H:i:s'),
                    'is_credit' => 0,
                    'is_sys' => 1
                ];
                pay::create($data);
            }
            Db::commit();
            return $this->msg($find);
        }catch (\Exception $e){
            Db::rollback();
            return $this->fail('fail',400,200);
        }
    }

    public function update(){
        $post = input('post.');
        $find = model::where('user_id', $this->userInfo->user_id)->find();
        $find->save($post);
        return $this->msg();
    }


    public function pay_url(){
        $info = p::where('pay_ds',1)
            ->find();
        return $this->msg($info);
    }

    public function amount_pay(){
        $find = p::where('pay_ds',1)->find();
        $function = 'pay_pay'.$find->td;
        $result = $this->$function();
        !empty(json_decode($result)) && $result = json_decode($result);
        $order = pay::find(input('id'));
        if (!empty($order->url))
            return $this->msg(['url' => $order->url]);
        if ($find->td == 1){
            if ($result->respCode == 'SUCCESS'){
                $order->save(['url' => $result->payInfo]);
                return $this->msg(['url' => $result->payInfo]);
            }else
                return $this->fail('FAIL:'.$result->tradeMsg);
        }elseif($find->td == 2){
            if (!empty($result->platformOrderCode)){
                $order->save(['url' => $result->platformOrderCode]);
                return $this->msg($result->paymentUrl);
            }else
                return $this->fail('FAIL');
        }
    }


    public function pay_pay1(){
        $id = input('id');
        $order = pay::find($id);
        $find = p::where('pay_ds',1)->find();
        $url = 'https://payment.qeapay.com/pay/web';
//        $key = 'a3a0b75ae9964c7ab08d8b8125cb6715';//测试
        $key = $find->pay_ds_key;
        $data = [
            //'mch_id' => '400002002',//测试
            'version' => '1.0',
            'mch_id' => $find->pay_no,
            'mch_order_no' => $order->order_code,
            'pay_type' => $find->pay_type,
            'order_date' => $order->create_time,
            'trade_amount' => floor($order->pay_amount),
            'goods_name' => 'thisisgoods'.$order->pay_amount,
            'notify_url' => $find->pay_ds_url
        ];
        ksort($data);
        foreach ($data AS $index => $value)
            $array[] = $index.'='.$value;
        $data['sign'] = MD5(implode('&',$array).'&key='.$key);
        $data['sign_type'] = 'MD5';
        $result = p::http_post_res($data,$url);
        return $result;
    }

    public function pay_pay2(){
        $id = input('id');
        $find = p::where('pay_ds',1)->find();
        $key = $find->pay_ds_key;
        $url = 'https://hxpayment.top/payment/collection';
        $order = pay::find($id)->toArray();
        $data = [
            'merchantLogin' => 'Blackcat',
            'orderCode' => $order['order_code'],//测试
            'amount' => bcadd($order['pay_amount'],0,2),
            'name' => $order['name'],
            'email' => $order['email'],
            'phone' => $order['phone']
        ];
        ksort($data);
        foreach ($data AS $index => $value)
            $array[] = $index.'='.$value;
        $data['sign'] = MD5(implode('&',$array).'&key='.$key);
        $result = p::http_post_res($data,$url,true);
//        $result = doCurlPostRequest($url,$data);
        return ($result);
    }
}